package com.itboye.core.server;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Binder;
import android.os.IBinder;

import com.itboye.core.broadcastreceiver.ByBroadCast;
import com.itboye.core.cache.SharedPreferenceCache;
import com.itboye.textileexpres.base.MainApplication;

/**
 * @author wushunhui 全局广播类
 */
@SuppressLint("NewApi")
public class ByService extends Service {

	private boolean isConnect;
	boolean temp;
	SharedPreferenceCache shareCache;
	private TimerTask mTimerTask;
	private Timer mTimer;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mTimer = new Timer();
		mTimerTask = new TimerTask() {
			@Override
			public void run() {
				isConnect = isConnectingToInternet();
				System.out.println(judgeWifiType()+"？？？？？？？？？");
				System.out.println("---> count=" + isConnect);
				Intent intent = new Intent(getApplicationContext(),
						ByBroadCast.class);
				intent.putExtra("isConnect", isConnect);
				sendBroadcast(intent);
			}
		};
		// 开始一个定时任务
		mTimer.schedule(mTimerTask, 2000, 1500);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	public class MyBinder extends Binder {

	}

	/**
	 * 判断网络状态
	 * 
	 * @return
	 */
	public static boolean isConnectingToInternet() {
		ConnectivityManager connectivity = (ConnectivityManager) MainApplication
				.getGetInstance()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			NetworkInfo netword2 = connectivity.getActiveNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}

		}
		return false;
	}

	/**
	 * 
	 */
	public String judgeWifiType() {
		/**
		 *  * NETWORK_TYPE_CDMA 网络类型为CDMA
         * NETWORK_TYPE_EDGE 网络类型为EDGE
         * NETWORK_TYPE_EVDO_0 网络类型为EVDO0
         * NETWORK_TYPE_EVDO_A 网络类型为EVDOA
         * NETWORK_TYPE_GPRS 网络类型为GPRS
         * NETWORK_TYPE_HSDPA 网络类型为HSDPA
         * NETWORK_TYPE_HSPA 网络类型为HSPA
         * NETWORK_TYPE_HSUPA 网络类型为HSUPA
         * NETWORK_TYPE_UMTS 网络类型为UMTS
         * 联通的3G为UMTS或HSDPA，移动和联通的2G为GPRS或EDGE，电信的2G为CDMA，电信
			的3G为EVDO
		 */
		ConnectivityManager connectivity = (ConnectivityManager) MainApplication.getGetInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo netword2 = connectivity.getActiveNetworkInfo();
			return netword2.getSubtype()+"";//判断类型
		}
		return null;
	}

	/**
	 * 
	 * android获取手机的ip地址
	 * 
	 * @return
	 */
	private String getPhoneIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()
							&& inetAddress instanceof Inet4Address) {
						// if (!inetAddress.isLoopbackAddress() && inetAddress
						// instanceof Inet6Address) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
		}
		return "";
	}
}
